/*
    Copyright (c);2019-2022;Wiscom System;

	All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the Wiscom System nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "los_hwi.h"
#include "los_task.h"
#include "los_api_bsp_gmc.h"
#include "los_inspect_entry.h"
#include "csg_sdk_hal.h"
#include "lwip/init.h"
#include "lwip/tcpip.h"
#include "netif/etharp.h"
#include "ethernetif.h"
#include "lwip/tcpip.h"
#include "pcram.h"
#include "lwip/sockets.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cpluscplus */
#endif /* __cpluscplus */

static UINT32 g_uwTskHiID;
static UINT32 g_uwTskLoID;
#define TSK_PRIOR_HI 4
#define TSK_PRIOR_LO 5
static UINT8 g_XEC_flag=1;

#define SERVER_ADDR "192.168.116.100"
#define SELF_ADDR "192.168.116.11"
#define SERVER_PORT 1886
#define BUFFER_SIZE 128

#define PORT           1886
#define IP_ADDR        "192.168.116.100"
#define RECV_DATA         (50)
static UINT32 TempData=0;
void Example_Lwip_TcpServer_Recv_Success(void)
{
dprintf("\r\n--------4.31 Lwip TcpServerRecv success test start!--------\r\n");

  int sock = -1,connected;
  char *recv_data;
  struct sockaddr_in server_addr,client_addr;
  socklen_t sin_size;
  int recv_data_len;
  char recv_buf[RECV_DATA]={0};
  recv_data = recv_buf;
  int i,j;
  if (recv_data == NULL)
  {
      printf("No memory\n");
      goto __exit;
  }
  
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0)
  {
      printf("Socket error\n");
      goto __exit;
  }
  
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = INADDR_ANY;
  server_addr.sin_port = htons(PORT);
 
  memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
  
  if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
  {
      printf("Unable to bind\n");
      goto __exit;
  }
  
  if (listen(sock, 5) == -1)
  {
      printf("Listen error\n");
      goto __exit;
  }
  

    sin_size = sizeof(struct sockaddr_in);
 
   // connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
	if ((connected = accept(sock, (struct sockaddr *)&client_addr, (socklen_t *)&sin_size)) == -1)
	{
		printf("accept error,errno 0x%x\r\n",errno);
		// continue;
	}
	
    printf("new client connected from (%s, %d)\n",
            inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
    {
      int flag = 1;
      
      setsockopt(connected,
                 IPPROTO_TCP,     /* set option at TCP level */
                 TCP_NODELAY,     /* name of option */
                 (void *) &flag,  /* the cast is historical cruft */
                 sizeof(int));    /* length of option value */
    }
    while(TempData!=0x55)
    {
      TempData=0;
      TempData=(USART0->RXDATA);
      if(TempData& USART_RXDATA_EMPTY)
      {
        TempData=0;
      }
      else
      {
        TempData=(TempData & 0xFF);
      }
      recv_data_len = recv(connected, recv_data, RECV_DATA, 0);
      
      if (recv_data_len <= 0);
        // break;
      
    //   printf("recv %d len data\n",recv_data_len);
      printf("%s", recv_data);
    //   send(connected,recv_data,recv_data_len,0);
        for(j=0;j<recv_data_len;j++)
        {
            *(recv_data+j)=0;
        }
    }

      

    if (connected >= 0) 
    {
        printf("shutdown and closesocket!\r\n");
        shutdown(connected, SHUT_RDWR);
        closesocket(connected);
    }
      
    
    connected = -1;
    
__exit:
  if (sock >= 0) closesocket(sock);
}


#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cpluscplus */
#endif /* __cpluscplus */
